<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="部门名字"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-select v-model="filters.parentId" clearable placeholder="父级部门">
                        <el-option
                                v-for="item in parentDepts"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-select v-model="filters.managerId" clearable placeholder="员工部门">
                        <el-option
                                v-for="item in deptManagers"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="getShop">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <!--		:data :绑定的数据-->
        <!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
        <!--		prop:对应的是:data绑定的对象的属性-->
        <!--		label :描述-->
        <el-table :data="dept" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="商铺名称" width="120" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
            </el-table-column>
            <el-table-column prop="logo" label="LOGO" width="100" sortable>
                <template slot-scope="scope">
                    <img :src="scope.row.logo" min-width="70" height="70"/>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="300">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleDel(scope.$index, scope.row)">
                        <el-row>
                            <el-button type="success">审核通过</el-button>
                        </el-row>
                    </el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                        <el-row>
                            <el-button type="danger">审核驳回</el-button>
                        </el-row>
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门编号" prop="sn">
                    <el-input v-model="editForm.sn" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!-- 审核模态款-->
        <el-dialog title="机构入驻审核" :visible.sync="dialogFormVisible">
            <el-form :model="shop">
                <el-form>

                    <el-row>

                        <el-col span="8">

                            <el-form-item label="机构名:" prop="name">
                                <el-input v-model="shop.name" auto-complete="off"></el-input>
                            </el-form-item>

                        </el-col>
                        &nbsp;&nbsp;&nbsp;
                        <el-col span="8">

                            <el-form-item label="机构状态:" prop="state">
                                <el-input v-model="shop.state" auto-complete="off"></el-input>
                            </el-form-item>

                        </el-col>

                    </el-row>

                </el-form>
                <el-form-item label="驳回类型" label-width="80px">
                    <el-select v-model="shop.content" placeholder="请选择">
                        <el-option label="就不同意" value="shanghai"></el-option>
                        <el-option label="店铺太烂" value="beijing"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click.native="shopSubmit" :loading="addLoading">确 定</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <!--				<el-form-item label="部门编号" prop="sn">-->
                <!--					<el-input v-model="addForm.sn" auto-complete="off"></el-input>-->
                <!--				</el-form-item>-->
                <el-form-item label="部门状态" prop="state">
                    <el-radio-group v-model="addForm.state">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="部门经理" prop="managerId">
                    <el-select v-model="addForm.managerId" clearable placeholder="请选择经理">
                        <el-option
                                v-for="item in employees"
                                :key="item.id"
                                :label="item.username"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="上级部门" prop="parentIds">
                    <el-cascader
                            v-model="addForm.parentIds"
                            :options="deptTree"
                            :props="{ checkStrictly: true ,label:'name',value:'id'}"
                            clearable></el-cascader>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>

<script>

    //import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    //import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    name: '',
                    parentId: '',
                    managerId: '',
                },
                shop: {
                    name: '',
                    state: 3,
                    logo: '',
                    content:'',
                },
                dept: [],
                deptTree: [],
                employees: [],
                parentDepts: [],
                deptManagers: [],
                total: 0,
                page: 1,
                size: 10,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                    name: '',
                    sn: ''
                },
                dialogFormVisible: false,
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },
                form: {
                    name: '',
                    region: '',
                    date1: '',
                    date2: '',
                    delivery: false,
                    type: [],
                    resource: '',
                    desc: ''
                },
                //新增界面数据
                addForm: {
                    name: '',
                    sn: '',
                    state: 1,
                    parentIds: [],
                }

            }
        },
        methods: {
            //性别显示转换
            //row 当前行row = user  column当前列
            formatSex: function (row, column) {
                return row.state == 1 ? '正常' : row.state == 0 ? '禁用' : '未知';
            },
            handleCurrentChange(val) {
                //var 就是当前页的数据
                this.page = val;
                this.getShop();
            },
            //获取部门列表
            getShop() {
                let shop = {
                    page: this.page,
                    name: this.shop.name,
                    state: this.shop.state,
                    logo: this.shop.logo
                };
                this.listLoading = true;
                //NProgress.start();
                this.$http.post("/shop/findPage", shop).then((res) => {
                    // console.log("后端发送过来的分页数据:"+JSON.stringify(res));
                    //
                    this.total = res.data.data.total;
                    //
                    this.dept = res.data.data.list;
                    //关闭进度条
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认通过吗?', '提示', {
                    type: 'success'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    //let para = { id: row.id };
                    this.$http.delete("/shop/" + row.id).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '审核成功',
                            type: 'success'
                        });
                        this.dialogFormVisible = false;
                        this.getShop();
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                //显示编辑页面的模态框
                this.dialogFormVisible = true;
                this.shop = row;
                //Object.assign 是拷贝对象 BeanUtil.copyof()
                console.log(row);
                console.log("当前修改的数据是:" + JSON.stringify(this.shop))
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    sn: '',
                    state: 1
                };
            },
            //新增
            addSubmit: function () {
                //$refs调用js 原生dom元素
                //validate
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //这里才是我们处理数据的地方
                            this.addLoading = true;
                            //Object.assign 复制对象的
                            let para = Object.assign({}, this.addForm);
                            this.$http.post("/dept", para).then((res) => {
                                console.log(res)
                                this.addLoading = false;
                                //elementUi的提示弹框
                                this.$message({
                                    message: res.data.success ? '操作成功' : res.data.message,
                                    type: res.data.success ? 'success' : 'error'
                                });
                                //重新set数据
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                //刷新数据

                                this.getShop();

                            });
                        });
                    }
                });
            },
            shopSubmit: function () {
                //$refs调用js 原生dom元素
                //validate
                this.$confirm('确认驳回吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    // let para = {ids: ids};
                    // console.log(ids)
                    this.$http.post("/shop/process",this.shop).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '驳回成功',
                            type: 'success'
                        });
                        this.dialogFormVisible = false;
                        this.getShop();
                    });
                }).catch(() => {

                });

            },
            //复选勾选框的方法
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id)//去掉tostring
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {ids: ids};
                    console.log(ids)
                    this.$http.post("/shop/batchRemove", para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getShop();
                    });
                }).catch(() => {

                });
            },
            //获取上级部门
            getParentDepts() {
                this.$http.get("/dept/findParentDepts").then(res => {
                    //console.log(res)
                    this.parentDepts = res.data.data;
                })
            },
            //获取部门经理,
            getDeptManagers() {
                this.$http.get("/emp/findManager").then(res => {
                    console.log(res)
                    this.deptManagers = res.data.data;
                })
            },
            //获取所以员工,
            getAllEmpolyee() {
                this.$http.get("/emp/findAll").then(res => {
                    console.log(res)
                    this.employees = res.data.data;
                })
            },
            //获取所以员工,
            getDeptTree() {
                this.$http.get("/dept/deptTree").then(res => {
                    console.log(res)
                    this.deptTree = res.data.data;
                })
            },
        },
        //就是页面已加载完成就会去触发这个mounted
        mounted() {
            this.getShop();
            this.getParentDepts();
            this.getDeptManagers();
            this.getAllEmpolyee();
            this.getDeptTree();
        }
    }

</script>

<style scoped>

</style>
